package com.chenchao.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/21 13:51
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.collection.Test01.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 模拟斗地主
 */

public class Test01 {
    public static void main(String[] args) {
        ArrayList<Player> players = new ArrayList<>();
        players.add(new Player("小红"));
        players.add(new Player("翁狗屎"));
        players.add(new Player("杨宝"));
        new Game(players,1).play();
    }
}

/**
 * ArrayList<Integer> integers发到的牌
 * name 玩家名称
 */
class Player{
    private String name="";
    private ArrayList<Integer> integers;

    public Player() {
    }

    public Player(String name) {
        this.name = name;
        integers=new ArrayList<>();
    }
    public void addCards(int cardNumber){
        this.integers.add(cardNumber);

    }
    public ArrayList<Integer> lookCards(){
        //看牌前先洗牌排序
        Collections.sort(this.integers);
        return  this.integers;
    }

    public String getName() {
        return name;
    }

    public ArrayList<Integer> getIntegers() {
        return integers;
    }
}

/**
 * PER_CARDS_COUNT =54 每套扑克牌54张
 * NUMBER_OF_HOLE_CARDS 底牌张数
 * 玩家集合 players
 * 牌集合 integerStringHashMap
 * 玩几套牌 playingCardsCount
 * 用于洗牌的牌号 cardsNumber
 */
class Game{
    private static final int PER_CARDS_COUNT =54;
    private static final int NUMBER_OF_HOLE_CARDS =3;
    private ArrayList<Player> players;
    private HashMap<Integer, String> integerStringHashMap;
    private int playingCardsCount;
    private ArrayList<Integer> cardsNumber;

    /**
     * 初始化
     * @param players   玩家集合
     * @param playingCardsCount 玩几套牌
     */
    public Game(ArrayList<Player> players, int playingCardsCount) {
        this.players = players;
        this.playingCardsCount = playingCardsCount;
        this.createCards();
        this.createCardsNumber();
    }

    public Game() {
    }

    /**
     * 创建扑克牌
     */
    public void createCards (){
        integerStringHashMap=new HashMap<>(PER_CARDS_COUNT*playingCardsCount);
        String[] str1={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] str2={"♣","♠","♦","♥"};
        int count=0;
        for (String s2 : str1) {
            for (String s1 : str2) {
                for (int i = 0; i < playingCardsCount; i++) {
                    integerStringHashMap.put(count++,s1.concat(s2));
                    //循环造出扑克牌，用到了字符串拼接
                }
            }
        }
        //放入大小王
        for (int i = 0; i < playingCardsCount; i++) {
            integerStringHashMap.put(count++,"小王");
        }
        for (int i = 0; i < playingCardsCount; i++) {
            integerStringHashMap.put(count++,"大王");
        }

    }

    /**
     * 创建牌号
     */
    public void   createCardsNumber(){
        cardsNumber=new ArrayList<>(playingCardsCount * PER_CARDS_COUNT);
        for (int i = 0; i < playingCardsCount * PER_CARDS_COUNT; i++) {
            cardsNumber.add(i);
        }
    }

    /**
     * 洗牌
     */
    public  void shuffle(){
        Collections.shuffle(cardsNumber);
    }

    /**
     * 发牌
     */
    public  void dealCards(){
        for (int i = 0; i < cardsNumber.size()-Game.NUMBER_OF_HOLE_CARDS ; i++) {
            int j=i% players.size();
            //获得该抓牌的人的编号
            players.get(j).addCards(cardsNumber.get(i));
        }
    }

    /**
     * 看牌
     */
    public void showCards(){
        //玩家看牌
        for (Player player : players) {
            StringBuilder stringBuilder=new StringBuilder(player.getName()+":");
            for (Integer lookCard :player.lookCards()) {
                stringBuilder.append(integerStringHashMap.get(lookCard));
                //拼接牌名称
                stringBuilder.append(" ");
            }
            System.out.println(stringBuilder.toString().trim());
            //剪去末尾多余的” “
        }

        //打印底牌
        StringBuilder stringBuilder=new StringBuilder("底牌:");
        for (int i = NUMBER_OF_HOLE_CARDS; i >0 ; i--) {
            stringBuilder.append(integerStringHashMap.get(cardsNumber.get(cardsNumber.size()-i)));
            stringBuilder.append(" ");
        }
        System.out.println(stringBuilder.toString().trim());
    }

    /**
     * 开始游戏
     */
    public  void play(){
        //洗牌
        shuffle();
        //发牌
        dealCards();
        //看牌
        showCards();
    }
}
